﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Sprites;
using UnityEngine.UI;

public class CircleImage : Image
{
    /// <summary>
    /// 圆形由多少块三角形组成
    /// </summary>

    [SerializeField]
    private int segements = 100;

    //显示部分占圆形的百分比
    [SerializeField]
    private float showPercent = 1;
    private readonly Color32 GRAY_COLOR = new Color32(60, 60, 60, 255);
    private List<Vector3> _vertexList;

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();

        _vertexList = new List<Vector3>();

        AddVertex(vh, segements);

        AddTriangle(vh, segements);
        

    }

    private void AddTriangle(VertexHelper vh, int realSegements)
    {
        int id = 1;
        for (int i = 0; i < realSegements; i++)
        {
            vh.AddTriangle(id, 0, id + 1);
            id++;
        }
    }

    private void AddVertex(VertexHelper vh, int segements)
    {
        #region 屏幕位置信息转换成uv顶点信息
        float width = rectTransform.rect.width;
        float height = rectTransform.rect.height;
        int realSegements = (int)(segements * showPercent);

        Vector4 uv = overrideSprite != null ? DataUtility.GetOuterUV(overrideSprite) : Vector4.zero;
        float uvWdith = uv.z - uv.x;
        float uvHeight = uv.w - uv.y;
        Vector2 uvCenter = new Vector2(uvWdith * 0.5f, uvHeight * 0.5f); //uv中心坐标
        Vector2 convertRatio = new Vector2(uvWdith / width, uvHeight / height);
        #endregion

        float radian = (2 * Mathf.PI) / segements;
        float radius = width * 0.5f;
        Vector2 originPos = new Vector2((0.5f - rectTransform.pivot.x) * width, (0.5f - rectTransform.pivot.y) * height);

        Vector2 vertPos = Vector2.zero;

        Color32 colorTemp = GetOriginColor();

        UIVertex orgin = GetUIVertex(colorTemp, originPos, vertPos, uvCenter, convertRatio);
       
        vh.AddVert(orgin);

        int vertexCount = realSegements + 1;
        float curRadian = 0;
        Vector2 posTemp;
        for (int i = 0; i < segements + 1; i++)
        {
            float x = Mathf.Cos(curRadian) * radius;
            float y = Mathf.Sin(curRadian) * radius;
            curRadian += radian;

            UIVertex vertexTemp = new UIVertex();
            if (i < vertexCount)
            {
                vertexTemp.color = color;
            }
            else
            {
                vertexTemp.color = new Color32(60, 60, 60, 255);
            }
            posTemp = new Vector2(x, y);
            vertexTemp.position = posTemp + originPos;
            vertexTemp.uv0 = new Vector2(posTemp.x * convertRatio.x + uvCenter.x, posTemp.y * convertRatio.y + uvCenter.y);
            vh.AddVert(vertexTemp);
            _vertexList.Add(posTemp+originPos);
        }
    }

    private UIVertex GetUIVertex(Color32 col, Vector3 pos, Vector2 uvPos, Vector2 uvCenter, Vector2 uvScale)
    {
        UIVertex vertexTemp = new UIVertex();
        vertexTemp.color = col;
        vertexTemp.position = pos;
        vertexTemp.uv0 = new Vector2(uvPos.x * uvScale.x + uvCenter.x, uvPos.y * uvScale.y + uvCenter.y);
        return vertexTemp;
    }

    private Color32 GetOriginColor()
    {
        Color32 colorTemp = (Color.white - GRAY_COLOR) * showPercent;
        return new Color32((byte)(GRAY_COLOR.r + colorTemp.r),
                            (byte)(GRAY_COLOR.g + colorTemp.g),
                            (byte)(GRAY_COLOR.b + colorTemp.b),
                            255
                    );
    }

    public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
    {
        Vector2 localPoint;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPoint, eventCamera, out localPoint);
//        return IsValid(localPoint);
    }

//    private bool IsValid(Vector2 localPoint)
//    {
////         return GetCrossPointNum(localPoint) % 2 == 1;
//    }
//
//    private int GetCrossPointNum(Vector2 localPoint, List<Vector3> vertexList)
//    {
//        for (int i = 0; i < vertexList.Count; i++)
//        {
////            _vertexList.Add();
//        }
//    }


}